package so.shanku.essential.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.utils.StringUtil;

/**
 * Created by Administrator on 2015/9/8.
 */
public class ChangePasswordActivity extends BaseUIActivity {
	/**
	 * 1.手机验证
	 */
	@ViewInject(id = R.id.tv_confirm_phone)
	private TextView tv_confirm_phone;
	/**
	 * 2.密码验证
	 */
	@ViewInject(id = R.id.tv_confirm_password)
	private TextView tv_confirm_password;
	/**
	 * 输入框的隐藏（手机验证）
	 */
	@ViewInject(id = R.id.linearlayout_confirm_phone)
	private LinearLayout linearlayout_confirm_phone;
	/**
	 * 输入手机号
	 */
	@ViewInject(id = R.id.et_input_phone)
	private EditText et_input_phone;
	/**
	 * 输入验证码
	 */
	@ViewInject(id = R.id.et_input_password)
	private EditText et_input_password;
	/**
	 * 发送验证码
	 */
	@ViewInject(id = R.id.btn_send_code, click = "sendpassword_click")
	private TextView tv_sendpassword;
	/**
	 * 输入框的隐藏（确认密码）
	 */
	@ViewInject(id = R.id.linearlayout_confirm_password)
	private LinearLayout linearlayout_confirm_password;
	/**
	 * 新密码
	 */
	@ViewInject(id = R.id.et_new_password)
	private EditText et_new_password;
	/**
	 * 确认密码
	 */
	@ViewInject(id = R.id.et_confirm_password)
	private EditText et_confirm_password;
	/**
	 * 下一步
	 */
	@ViewInject(id = R.id.bt_confirm, click = "bt_confirm_click")
	private TextView bt_confirm;
	private boolean isFirst;
	/**
	 * 定时器
	 */
	private TimeCount time;
	private String passWord;
	private String confirmPassWord;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		initActivity(true, R.drawable.back, null, R.string.change_password, -1, null);
		isFirst = true;
		time = new TimeCount(120000, 1000);

	}

	public void bt_confirm_click(View view) {
		if (isFirst) {
				if (nextStepJudge()) {
					getData_UserSubmitCode();
					getData_UserSubmitCode_Success();
				}

		} else {
			if(confirmChangeJudge()){
				getData_ConfirmChange();
				getData_ConfirmChange_Success();
			}
		}

	}
	    //当getData_UserSubmitCode成功后调用
	public void getData_UserSubmitCode_Success(){
		linearlayout_confirm_phone.setVisibility(View.GONE);
		tv_confirm_password.setBackgroundResource(R.drawable.confirm_password);
		tv_confirm_phone.setBackgroundResource(R.color.white);
		linearlayout_confirm_password.setVisibility(View.VISIBLE);
		bt_confirm.setText(R.string.confirm_change);
		isFirst = false;
	}
	//当 getData_ConfirmChange成功后调用
	public void  getData_ConfirmChange_Success(){
		linearlayout_confirm_phone.setVisibility(View.VISIBLE);
		tv_confirm_phone.setBackgroundResource(R.drawable.confirm_phone);
		tv_confirm_password.setBackgroundResource(R.color.white);
		linearlayout_confirm_password.setVisibility(View.GONE);
		bt_confirm.setText(R.string.next);
		isFirst = true;
	}

	/**
	 * 判断手机号码
	 */
	private String userPhone;

	public boolean judgeMobileNo() {
		boolean isjudge = true;
		userPhone = et_input_phone.getText().toString();
		if (userPhone == null || "".equals(userPhone)) {
			//手机号码为空的情况
			toast.showToast(R.string.register_name_null);
			isjudge = false;
		} else if (userPhone.length() < 11) {
			toast.showToast(R.string.phone_formal_error);
			isjudge = false;
		}
		return isjudge;
	}




	/**
	 * 发送验证码获取数据
	 */
	private void getData_UserSendCode() {

	}

	/**
	 * 下一步获取数据（提交验证码）
	 */
	public void getData_UserSubmitCode() {

	}
	/**
	 * 确认修改
	 */
	public void getData_ConfirmChange() {

	}

	/**
	 * 点击发送验证码
	 */
	public void sendpassword_click(View view) {
		if (judgeMobileNo()) {
//			getData_UserSendCode();
		}
	}

	private String getCode;

	//验证码倒计时
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			//参数依次为总时长,和计时的时间间隔
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			//计时完毕时触发
			tv_sendpassword.setText(getString(R.string.revalidation));
			tv_sendpassword.setClickable(true);
			getCode = "";
		}

		@Override
		public void onTick(long millisUntilFinished) {//计时过程显示
			tv_sendpassword.setClickable(false);
			tv_sendpassword.setText(millisUntilFinished / 1000 + getString(R.string.second));
		}
	}

	/**
	 * 点击下一步的判断
	 */
	public boolean nextStepJudge() {
		boolean isjudge = true;
		boolean captcha = true;
		getCode = et_input_password.getText().toString();
		if (getCode == null || "".equals(getCode)) {
			toast.showToast(R.string.pls_input_code);
			captcha = false;
		}
		if (judgeMobileNo() && captcha) {
			isjudge = true;
		} else {
			isjudge = false;
		}
		return isjudge;
	}
	/**
	 * 确认修改的判断
	 */
	public boolean confirmChangeJudge() {
		boolean isjudge = true;
		boolean captcha = true;

		passWord= et_new_password.getText().toString();
		confirmPassWord = et_confirm_password.getText().toString();
		if(TextUtils.isEmpty(confirmPassWord)){
			toast.showToast(R.string.pls_input_confirm_pw);
			captcha = false;
		}else{
			if (!confirmPassWord.equals(passWord)) {
				toast.showToast(R.string.password_differ);
				captcha = false;
		}

		}
		if (isPasswordAuthentication(passWord) && captcha) {
			isjudge = true;
		} else {
			isjudge = false;
		}
		return isjudge;
	}
	/**
	 * 密码判断
	 */
	public boolean isPasswordAuthentication(String password){
		if(!StringUtil.passwordAuthenticationLength(password)){
			toast.showToast(R.string.register_passwordRule);
			return false;
		} else if (!StringUtil.passwordAuthentication(password)){
			toast.showToast(R.string.password_format_error);
			return false;
		}
		return true;
	}
}
